﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.Sales
{	public partial class CheckReturns : Form
	{	private ToolStripMenuItem menuItem = null;

		public CheckReturns() : this(null)
		{
		}

		public CheckReturns(ToolStripMenuItem menuItem)
		{	InitializeComponent();

			this.menuItem = menuItem;
			if(menuItem != null)
				menuItem.Tag = this;
		}

		private void CheckReturns_FormClosed(object sender, FormClosedEventArgs e)
		{	if(menuItem != null)
				menuItem.Tag = null;
		}
//-------------------------------------------------------------------------------------------------
		private void CheckReturns_Load(object sender, EventArgs e)
		{	toolStripButtonView.Enabled = false;
			toolStripRefresh_Click(toolStripButtonRefresh, EventArgs.Empty);
		}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
		private void toolStripRefresh_Click(object sender, EventArgs e)
		{	try
			{	checkReturnsTableAdapter.Fill(sales.CheckReturns);
			}
			catch(SqlException eq)
			{	MessageBox.Show(this, string.Format("{0} ({1}): {2}", eq.Procedure, eq.LineNumber, eq.Message), Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			catch(Exception ex)
			{	MessageBox.Show(this, ex.Message, Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}
//-------------------------------------------------------------------------------------------------
		private void toolStripView_Click(object sender, EventArgs e)
		{	if(checkReturnsGridView.CurrentCell != null)
			{	CheckReturn checkReturn = new CheckReturn((DIProject.DataSets.Sales.CheckReturnsRow)((DataRowView)checkReturnsGridView.CurrentCell.OwningRow.DataBoundItem).Row);
				checkReturn.ShowDialog(this);
			}
		}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
		private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
		{	if(clickedCell != null)
			{	viewToolStripMenuItem.Enabled = true;
			}
			else
			{	viewToolStripMenuItem.Enabled = false;
			}
		}
//-------------------------------------------------------------------------------------------------
		private DataGridViewCell clickedCell = null;
		private void checkReturnsGridView_MouseDown(object sender, MouseEventArgs e)
		{	clickedCell = null;
			if(e.Button == MouseButtons.Right)
			{	DataGridView.HitTestInfo hit = checkReturnsGridView.HitTest(e.X, e.Y);
				if(hit.Type == DataGridViewHitTestType.Cell)
					checkReturnsGridView.CurrentCell = clickedCell = checkReturnsGridView.Rows[hit.RowIndex].Cells[hit.ColumnIndex];
			}
		}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
		private void checkReturnsGridView_CurrentCellChanged(object sender, EventArgs e)
		{	if(checkReturnsGridView.CurrentCell != null)
			{	toolStripButtonView.Enabled = true;
			}
			else
			{	toolStripButtonView.Enabled = false;
			}
		}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
		private void checkReturnsGridView_KeyDown(object sender, KeyEventArgs e)
		{	if(e.KeyCode == Keys.Enter)
				e.Handled = true;
		}
//-------------------------------------------------------------------------------------------------
		private void checkReturnsGridView_KeyPress(object sender, KeyPressEventArgs e)
		{	if(e.KeyChar == '\r' && checkReturnsGridView.CurrentCell != null)
				toolStripView_Click(toolStripButtonView, EventArgs.Empty);
		}
//-------------------------------------------------------------------------------------------------
		private void checkReturnsGridView_MouseDoubleClick(object sender, MouseEventArgs e)
		{	DataGridView.HitTestInfo hit = checkReturnsGridView.HitTest(e.X, e.Y);
			if(hit.Type == DataGridViewHitTestType.Cell)
			{	checkReturnsGridView.CurrentCell = clickedCell = checkReturnsGridView.Rows[hit.RowIndex].Cells[hit.ColumnIndex];
				toolStripView_Click(toolStripButtonView, EventArgs.Empty);
			}
		}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
	}
}